import java.io.*;

public class Main {
    private static String FILE_NAME="temp";
    public static void main(String[] args) throws Exception {
        User user=new User("");
        user.setName("小马哥");
        user.setSex("小帅锅");
        System.out.println(user);

        //将对象写入文件
        ObjectOutputStream objectOutputStream=
                new ObjectOutputStream(new FileOutputStream(FILE_NAME));
        objectOutputStream.writeObject(user);
        //注意，正确做法应该使用try----finally来关闭
        objectOutputStream.close();

        //为了看出static没有被实例化，修改age的值
        User.age="20";

        //将对象从文件红读取到内存中
        File file=new File(FILE_NAME);
        ObjectInputStream  objectInputStream=new ObjectInputStream(new FileInputStream(file));
        User oldUser= (User) objectInputStream.readObject();
        System.out.println("从文件读取的对象"+oldUser);

    }
}
